<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <!-- 引入 Tailwind CSS -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <!-- 引入 Axios 和 Vue -->
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
</head>
<body class="bg-gradient-to-r from-purple-500 to-blue-500 flex items-center justify-center min-h-screen">
    <div class="bg-white p-8 rounded-lg shadow-lg w-full max-w-sm">
        <h1 class="text-2xl font-bold text-center mb-6 text-gray-800">登录页面</h1>
        <form @submit.prevent="login">
            <input type="text" placeholder="用户名" v-model="user.username" required class="w-full p-3 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-blue-400 transition duration-200 ease-in-out" />
            <input type="password" placeholder="密码" v-model="user.password" required class="w-full p-3 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-blue-400 transition duration-200 ease-in-out" />
            <button type="submit" class="w-full bg-blue-500 text-white py-2 rounded-lg hover:bg-blue-600 transition duration-200 ease-in">登录</button>
        </form>
        <div class="mt-4 text-center">
            <p class="text-gray-600 text-sm">没有账号？<a href="/reg.html" class="text-blue-500 hover:underline">注册</a></p>
        </div>
    </div>

    <script>
        new Vue({
            el: "body > div",
            data() {
                return {
                    user: { username: "", password: "" }
                };
            },
            methods: {
                login() {
                    if (!this.user.username || !this.user.password) {
                        alert("请填写用户名和密码");
                        return;
                    }

                    axios.post("/v1/users/login", this.user)
                        .then(response => {
                            if (response.data === 1) {
                                alert("登录成功");
                                location.href = "/";
                            } else if (response.data === 2) {
                                alert("密码错误");
                            } else {
                                alert("用户名不存在");
                            }
                        })
                        .catch(error => {
                            console.error("登录请求错误:", error);
                            alert("服务器错误，请稍后重试");
                        });
                }
            }
        });
    </script>
</body>
</html>